﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Mengsk.ERP.Orders;
using Mengsk.ERP.Orders.TDOrderInterfaces;
using System.Diagnostics;
using Mengsk.ERP.Database;
using Microsoft.Win32;

namespace Mengsk.ERP.App.Orders
{
    /// <summary>
    /// OrderDetailUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class OrderDetailUserControl : UserControl
    {
        private Order[] orders = null;

        public Order[] Orders
        {
            get { return orders; }
            set
            {
                this.orders = value;
                this.lstOrders.ItemsSource = null;
                this.lstOrders.ItemsSource = value;
            }
        }

        public OrderDetailUserControl()
        {
            InitializeComponent();
        }

        private void btnQueryDelivery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Order order = (sender as Button).DataContext as Order;
                if (order == null)
                {
                    MessageBox.Show("程序不对，没有相应的Order");
                    return;
                }

                if (string.IsNullOrWhiteSpace(order.DeliveryCompany) || string.IsNullOrWhiteSpace(order.DeliveryNumber))
                {
                    MessageBox.Show("物流公司为空，或者相应的订单编号为空");
                    return;
                }

                for (int i = 0; i < 5; i++)
                {
                    try
                    {
                        string deliveryTrack = TDOrderInterfaceBase.GetOrderInterfaceSource(order.Shop).GetDeliveryTrack(order.POPID, order.DeliveryCompany, order.DeliveryNumber);
                        Console.WriteLine(deliveryTrack);
                        MessageBox.Show(deliveryTrack);
                        break;
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                        if (i >= 4)
                        {
                            throw new Exception(ex.Message);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnUpdateDelivery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Button btn = sender as Button;
                Order order = btn.DataContext as Order;
                TextBox tb = (btn.Parent as StackPanel).FindName("tbDeliveryNumber") as TextBox;
                TextBox tbDeliveryCompany = (btn.Parent as StackPanel).FindName("tbDeliveryCompany") as TextBox;

                string deliveryCompany = tbDeliveryCompany.Text.Trim();
                string deliveryNumber = tb.Text.Trim();

                if (order == null)
                {
                    MessageBox.Show("程序不对，没有相应的Order");
                    return;
                }

                if (string.IsNullOrWhiteSpace(deliveryNumber))
                {
                    MessageBox.Show("物流单号不能为空");
                    return;
                }

                if(string.IsNullOrWhiteSpace(deliveryCompany))
                {
                    MessageBox.Show("物流公司不能为空");
                    return;
                }

                if (MessageBox.Show("是否要更新物流公司与编号?", "警告", MessageBoxButton.YesNo, MessageBoxImage.Asterisk) != MessageBoxResult.Yes)
                {
                    return;
                }

                order.DeliveryNumber = deliveryNumber;
                order.DeliveryCompany = deliveryCompany;
                order.LastUpdateTimeErp = DateTime.Now;
                order.Comment = "手动更新物流";
                DBHelper.Instance.Update("", OrderManager.TABLE_ORDER, "ERPID='" + order.ERPID + "'", null, order, true, "DeliveryCompany", "DeliveryNumber", "LastUpdateTimeErp", "Comment");
                MessageBox.Show("已成功更新");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnStartPPCRM_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Microsoft.Win32.RegistryKey key = Microsoft.Win32.RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.ClassesRoot, Microsoft.Win32.RegistryView.Default);
                RegistryKey hTen = key.OpenSubKey("Tencent");

                if (hTen == null)
                {
                    throw new Exception("没有找到注册信息 HKEY_CLASSES_ROOT\\Tencent");
                }

                string programPath = hTen.GetValue("URL Protocol", "").ToString();

                if (string.IsNullOrWhiteSpace(programPath))
                {
                    throw new Exception("注册表中的QQ启动程序为空");
                }
                Button btn = sender as Button;
                Order order = btn.DataContext as Order;
                if (order == null)
                {
                    MessageBox.Show("没有选择用户");
                    return;
                }
                Process.Start("\"" + programPath + "\"", string.Format("\"tencent://message/?uin={0}&Site=拍拍网&Menu=yes\"", order.Name));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
